//
//  ADView.m
//  Meng
//
//  Created by dllo on 15/9/4.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ADView.h"
#import <UIImageView+WebCache.h>

#define TIME 3



@interface ADView ()<UIScrollViewDelegate>



/** 轮播图所用的所用图片 */
@property (nonatomic , strong) NSMutableArray *dataArrM;


/** 轮播图所用的所用图片 */
@property (nonatomic , strong) NSMutableArray *titleArrM;

/** 计时器*/
@property (nonatomic , strong) NSTimer *timer;


/*当前轮播图页数*/
@property (nonatomic , assign) NSInteger page;


@end


@implementation ADView

-(instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr titleArr:(NSArray *)titleArr{
    
    self = [super initWithFrame:frame];
    
    
    if (self) {
        
        self.scrollView = [[UIScrollView alloc] init];
        self.scrollView.delegate = self;
        [self addSubview:_scrollView];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
       
        self.page = 1;
        
        self.dataArrM = [NSMutableArray array];
        self.titleArrM = [NSMutableArray array];

        [self.dataArrM addObject:[dataArr lastObject]];
        [self.dataArrM addObjectsFromArray:dataArr];
        [self.dataArrM addObject:[dataArr firstObject]];
        
        [self.titleArrM addObject:[titleArr lastObject]];
        [self.titleArrM addObjectsFromArray:titleArr];
        [self.titleArrM addObject:[titleArr firstObject]];
        
        
        _scrollView.contentSize = CGSizeMake(self.frame.size.width*([_dataArrM count]), self.frame.size.height);
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:TIME target:self selector:@selector(autoScroll) userInfo:nil repeats:true];
        
        NSRunLoop *reunloop = [NSRunLoop currentRunLoop];
        [reunloop addTimer:_timer forMode:NSRunLoopCommonModes];
        
        
    }

    return self;
}

#pragma mark -布局子视图
-(void)layoutSubviews{
    self.scrollView.frame = self.frame;
    
    for (int i = 0; i<_dataArrM.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
  
        self.lab = [[UILabel alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 150 * Height, self.frame.size.width, 50 * Height)];
        
        [imageView sd_setImageWithURL:_dataArrM[i] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        self.lab.text = [_titleArrM objectAtIndex:i];
        self.lab.textAlignment = NSTextAlignmentCenter;
        self.lab.textColor = [UIColor whiteColor];
        
        [self.lab setShadowColor:[UIColor blackColor]];
        [self.lab setShadowOffset:CGSizeMake(-1, -1)];
        self.lab.font = [UIFont systemFontOfSize:14];

        [self.scrollView addSubview:imageView];
        [self.scrollView addSubview:_lab];
        
        
    }
    
    
    _scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    
}

#pragma mark - 自动滚动图片
-(void)autoScroll{
    
    self.page++;
    
    [self.scrollView setContentOffset: CGPointMake(self.frame.size.width*self.page, 0) animated:true];
    
    if (self.page == [_dataArrM count]-2) {
        
        [self.scrollView setContentOffset: CGPointMake(self.frame.size.width*self.page+1, 0) animated:true];
        
        /* 图片延迟1.5秒出现 */
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.scrollView setContentOffset: CGPointMake(0, 0) animated:false];
        });
        self.page = 0;
    }
    
   
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    self.page = scrollView.contentOffset.x/self.frame.size.width;
    
    /* 休眠  */
    [self.timer invalidate];
    
    _timer = nil;
    
    
}




#pragma mark - 结束滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width*([_dataArrM count]-2), 0);
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width*([_dataArrM count]-1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        
    }
    
    
    self.page = scrollView.contentOffset.x/self.frame.size.width;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:TIME target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    
    /* 在拉动的时候,也可以接着翻页 */
    NSRunLoop *reunloop = [NSRunLoop currentRunLoop];
    
    [reunloop addTimer:_timer forMode:NSRunLoopCommonModes];
    
  
}





@end
